package com.glacier.thread;

import com.glacier.thread.Stock;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
 * date 2024-01-02 17:02
 *
 * @author glacier
 * @version 1.0
 */
@Data
@ToString
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
public class MySyncThread extends Thread {

    public static final Object LOCK = new Object();

    private final Stock stock;

    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        synchronized (LOCK) {
            if (stock.getCount() > 0) {
                stock.decrement();
                System.out.println(Thread.currentThread().getName() + " count = " + stock.getCount());
            }
        }
    }

}
